package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Transactional //0.开启事物（涉及到多张表的增删改操作需要开事物）
@Override
public void addDish(DishDTO dto) {

    //1.构造菜品基本信息数据，将其存入dish表中
    Dish dish = new Dish();

    //拷贝属性值
    BeanUtils.copyProperties(dto, dish);
    //调用mapper保存方法
    dishMapper.insert(dish);

    log.info("dishid={}", dish.getId());
    //2.构造菜品口味列表数据，将其存入dish_flavar表中
    List<DishFlavor> dishFlavorList = dto.getFlavors();

    if (dishFlavorList != null && dishFlavorList.size() > 0) {
        //2.1 关联菜品id
        dishFlavorList.forEach(df -> {
            df.setDishId(dish.getId());
        });
        //2.2 调用mapper保存方法，批量插入口味列表数据
        dishFlavorMapper.insetBatch(dishFlavorList);
    }


}

    @Override
    public PageResult page(DishPageQueryDTO dto) {
        // 1。设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        // 2。调用mapper的列表查询方法，强转 成page
       Page<DishVO> page= dishMapper.list(dto);

       //3.封装pageResult对象并返回
        return new PageResult(page.getTotal(), page.getResult());

    }

    @Override
    @Transactional //0.开启事务
    public void delete(List<Long> ids) {
        // 1.删除菜品之前，需要判断菜品是否启售，启售中不允许删除
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //2.需要判断菜品是否被套餐关联，关联李也不允许删除

        Integer count = setmealDishMapper.countByDishId(ids);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3.删除菜品基本信息dish表
        dishMapper.deleteBatch(ids);
        //4.删除菜品口味列表信息 dish_flavor表
        dishFlavorMapper.deleteBatch(ids);
    }

    @Override
    public DishVO getById(Long id) {

        DishVO dishVO = new DishVO();
        //1.根据菜品id查询菜品基本信息,封装到dishVo中
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);

        //2.根据菜品id查询口味列表
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);
        //3.构造DishVo对象并返回
        return dishVO;
    }

    @Override
    @Transactional //0.开启事务
    public void update(DishDTO dto) {
         Dish dish = new Dish();
         BeanUtils.copyProperties(dto, dish);

         //1.修改菜品到基本信息，dish表
        dishMapper.update(dish);

        //2。修改口味列表信息， dish_flavor表
        //由于口味数据可能怎回家，可能删除，还可能修改口味到值，涉及到曾删改操作，所以先全部删除旧数据，再添加新数据
             dishFlavorMapper.deleteByDishId(dto.getId());

            List<DishFlavor> dishFlavorList = dto.getFlavors();
            //必须判断空，因为菜品新增时口味有可能被删除一旦删除 还去执行插入口味 回出现语法错误 insert into values
            if (dishFlavorList.size() > 0 && dishFlavorList !=null) {
                dishFlavorList.forEach(df -> {
                    df.setDishId(dish.getId());
                });
                dishFlavorMapper.insetBatch(dishFlavorList);
            }

    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listBy(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
    /**
     * 菜品起售停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }
}

